using System.Collections.Generic;
using UnityEngine;
using System.IO;

// 定义LootItems类
public class LootItems
{
    public string ItemCode { get; set; }
    public int Number { get; set; }
    public float Probability { get; set; }

    public LootItems(string itemCode, int number, float probability)
    {
        ItemCode = itemCode;
        Number = number;
        Probability = probability;
    }
}

public class DistortionProbability : MonoBehaviour
{

    // 用于读取全部道具的信息（从CSV文件加载）
    private Dictionary<string, string> ItemsLibrary = new Dictionary<string, string>();

    // 增强道具池
    private Dictionary<ItemRarity, List<string>> powerUpItemPools = new Dictionary<ItemRarity, List<string>>();
    // 功能道具池
    private Dictionary<ItemRarity, List<string>> functionalItemPools = new Dictionary<ItemRarity, List<string>>();
    public ItemManager itemManager;
    
    // ItemManager中保存的道具列表
    public List<FunctionalItem> functionalItems = new List<FunctionalItem>();
    public List<PowerUpItem> powerUpItems = new List<PowerUpItem>();

    // 根据失真度获取基础道具编号列表及其对应概率
    public List<LootItems> GetBasicItemsByDistortionLevel(int distortionLevel)
    {
        List<LootItems> items = new List<LootItems>();
        if (distortionLevel >= 0 && distortionLevel < 100)
        {
            items.Add(new LootItems("X001", 1, 0.20f));
            items.Add(new LootItems("X101", 1, 0.20f));
            items.Add(new LootItems("X201", 2, 0.20f));
            items.Add(new LootItems("X201", 3, 0.20f));
            items.Add(new LootItems("X202", 1, 0.20f));
        }
        else if (distortionLevel >= 100 && distortionLevel < 200)
        {
            items.Add(new LootItems("X001", 2, 0.20f));
            items.Add(new LootItems("X101", 2, 0.20f));
            items.Add(new LootItems("X001", 1, 0.20f));
            items.Add(new LootItems("X101", 1, 0.20f));
            items.Add(new LootItems("X202", 1, 0.20f));
        }
        else if (distortionLevel >= 200 && distortionLevel < 300)
        {
            items.Add(new LootItems("X002", 1, 0.20f));
            items.Add(new LootItems("X102", 1, 0.20f));
            items.Add(new LootItems("X001", 2, 0.20f));
            items.Add(new LootItems("X101", 2, 0.20f));
            items.Add(new LootItems("X202", 2, 0.20f));
        }
        else if (distortionLevel >= 300 && distortionLevel < 400)
        {
            items.Add(new LootItems("X003", 1, 0.20f));
            items.Add(new LootItems("X103", 1, 0.20f));
            items.Add(new LootItems("X002", 1, 0.20f));
            items.Add(new LootItems("X102", 1, 0.20f));
            items.Add(new LootItems("X203", 1, 0.20f));
        }
        else if (distortionLevel >= 400 && distortionLevel < 500)
        {
            items.Add(new LootItems("X003", 1, 0.20f));
            items.Add(new LootItems("X002", 1, 0.20f));
            items.Add(new LootItems("X103", 1, 0.20f));
            items.Add(new LootItems("X102", 1, 0.20f));
            items.Add(new LootItems("X203", 2, 0.20f));
        }
        else if (distortionLevel >= 500)
        {
            items.Add(new LootItems("X004", 1, 0.20f));
            items.Add(new LootItems("X104", 1, 0.20f));
            items.Add(new LootItems("X203", 4, 0.20f));
            items.Add(new LootItems("X204", 1, 0.20f));
            items.Add(new LootItems("X203", 2, 0.20f));
        }

        return items;
    }

    // 根据失真度获取增强道具概率
    public Dictionary<ItemRarity, float> GetPowerUpItemProbabilityByDistortionLevel(int distortionLevel)
    {
        var probabilities = new Dictionary<ItemRarity, float>();
        
        if (distortionLevel >= 0 && distortionLevel < 100)
        {
            probabilities[ItemRarity.Common] = 0.8f;
            probabilities[ItemRarity.Rare] = 0.2f;
            probabilities[ItemRarity.Epic] = 0.0f;
            probabilities[ItemRarity.Legendary] = 0.0f;
        }
        else if (distortionLevel >= 100 && distortionLevel < 200)
        {
            probabilities[ItemRarity.Common] = 0.5f;
            probabilities[ItemRarity.Rare] = 0.4f;
            probabilities[ItemRarity.Epic] = 0.1f;
            probabilities[ItemRarity.Legendary] = 0.0f;
        }
        else if (distortionLevel >= 200 && distortionLevel < 300)
        {
            probabilities[ItemRarity.Common] = 0.2f;
            probabilities[ItemRarity.Rare] = 0.5f;
            probabilities[ItemRarity.Epic] = 0.3f;
            probabilities[ItemRarity.Legendary] = 0.0f;
        }
        else if (distortionLevel >= 300 && distortionLevel < 400)
        {
            probabilities[ItemRarity.Common] = 0.0f;
            probabilities[ItemRarity.Rare] = 0.5f;
            probabilities[ItemRarity.Epic] = 0.4f;
            probabilities[ItemRarity.Legendary] = 0.1f;
        }
        else if (distortionLevel >= 400 && distortionLevel < 500)
        {
            probabilities[ItemRarity.Common] = 0.0f;
            probabilities[ItemRarity.Rare] = 0.3f;
            probabilities[ItemRarity.Epic] = 0.5f;
            probabilities[ItemRarity.Legendary] = 0.2f;
        }
        else if (distortionLevel >= 500)
        {
            probabilities[ItemRarity.Common] = 0.0f;
            probabilities[ItemRarity.Rare] = 0.0f;
            probabilities[ItemRarity.Epic] = 0.7f;
            probabilities[ItemRarity.Legendary] = 0.3f;
        }

        return probabilities;
    }

    // 根据失真度获取功能道具概率
    public Dictionary<ItemRarity, float> GetFunctionalItemProbabilityByDistortionLevel(int distortionLevel)
    {
        var probabilities = new Dictionary<ItemRarity, float>();
        if (distortionLevel >= 0 && distortionLevel < 200)
        {
            probabilities[ItemRarity.Rare] = 1.0f;
            probabilities[ItemRarity.Epic] = 0.0f;
            probabilities[ItemRarity.Legendary] = 0.0f;
        }
        else if (distortionLevel >= 200 && distortionLevel < 400)
        {
            probabilities[ItemRarity.Rare] = 0.0f;
            probabilities[ItemRarity.Epic] = 1.0f;
            probabilities[ItemRarity.Legendary] = 0.0f;
        }
        else if (distortionLevel >= 400)
        {
            probabilities[ItemRarity.Rare] = 0.0f;
            probabilities[ItemRarity.Epic] = 0.0f;
            probabilities[ItemRarity.Legendary] = 1.0f;
        }

        return probabilities;
    }
    private void Start()
    {
        // 示例中加载道具数据的调用
        //LoadItemDataFromCSV("path_to_your_csv_file.csv");

        // 填充道具池
        
        // 获取 ItemManager 引用
        itemManager = FindObjectOfType<ItemManager>();
        if (itemManager == null)
        {
            Debug.LogError("ItemManager not found!");
            return;
        }

        functionalItems = itemManager.functionalItems;
        powerUpItems = itemManager.powerUpItems;
        
        FillItemPools();
    }
    
    // 根据概率从道具池中选择一个增强道具
    public string GetRandomPowerUpItem(int distortionLevel)
    {
        // 根据当前失真度，获取不同稀有度增强道具的概率
        Dictionary<ItemRarity, float> probabilities = GetPowerUpItemProbabilityByDistortionLevel(distortionLevel);
        float randomValue = Random.Range(0f, 1f);
    
        // 计算累积概率并选择对应的道具
        float cumulativeProbability = 0f;
        
        foreach (var probability in probabilities)
        {
            cumulativeProbability += probability.Value;
            if (randomValue <= cumulativeProbability)
            {
                // 检查 powerUpItemPools 是否包含当前稀有度键
                // if (!powerUpItemPools.ContainsKey(probability.Key))
                // {
                //     Debug.LogError($"稀有度 '{probability.Key}' 不存在于 powerUpItemPools 中！");
                //     
                //     return null; // 或者返回一个默认值以避免崩溃
                // }
                // 从对应稀有度的道具池中随机选择一个道具
                return GetRandomItemFromPool(probability.Key, powerUpItemPools[probability.Key]);
            }
        }
        return null;
    }

    // 根据概率从道具池中选择一个功能道具
    public string GetRandomFunctionalItem(int distortionLevel)
    {
        // 获取不同稀有度功能道具的概率
        Dictionary<ItemRarity, float> probabilities = GetFunctionalItemProbabilityByDistortionLevel(distortionLevel);
        float randomValue = Random.Range(0f, 1f);

        // 计算累积概率并选择对应的道具
        float cumulativeProbability = 0f;
        foreach (var probability in probabilities)
        {
            cumulativeProbability += probability.Value;
            if (randomValue <= cumulativeProbability)
            {
                // 从对应稀有度的道具池中随机选择一个道具
                return GetRandomItemFromPool(probability.Key, functionalItemPools[probability.Key]);
            }
        }
        return null;
    }

    // 从对应稀有度的道具池中随机选择一个道具(道具池子品质，对应品质的池子)
    private string GetRandomItemFromPool(ItemRarity rarity, List<string> itemPool)
    {
        // 根据选定的稀有度筛选道具池中的道具并随机选择一个
        List<string> filteredPool = itemPool;
        string selectedItem = filteredPool[Random.Range(0, filteredPool.Count)];
        // 打印调试日志
        Debug.Log($"选中的道具的id: {selectedItem}，稀有度: {rarity}");
        
        return selectedItem;
    }

    // 自动化填充增强道具池和功能道具池
    private void FillItemPools()
    {
        // 填充增强道具池
        foreach (var item in powerUpItems)
        {
            if (!powerUpItemPools.ContainsKey(item.itemRarity))
            {
                powerUpItemPools[item.itemRarity] = new List<string>();
            }
            
            powerUpItemPools[item.itemRarity].Add(item.code);
        }

        // 填充功能道具池
        foreach (var item in functionalItems)
        {
            if (!functionalItemPools.ContainsKey(item.itemRarity))
            {
                functionalItemPools[item.itemRarity] = new List<string>();
                Debug.Log("11111");
            }
            functionalItemPools[item.itemRarity].Add(item.code);
        }
    }

    // 从CSV文件中加载道具数据
    private void LoadItemDataFromCSV(string filePath)
    {
        using (var reader = new StreamReader(filePath))
        {
            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                var values = line.Split(',');
                if (values.Length >= 2)
                {
                    string itemCode = values[0];
                    string itemDescription = values[1];
                    ItemsLibrary[itemCode] = itemDescription;
                }
            }
        }
    }

    
}
